package com.jrx.anytxn.transaction.timer.job.allocation.config;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlThirdPartyPaymentMapper;
import com.jrx.anytxn.transaction.service.IThirdPartyPaymentService;
import com.jrx.anytxn.transaction.timer.config.AnytxnBatchProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 三方还款分配
 *
 * @author lbz
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class ThirdPartyAlloctionConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(ThirdPartyAlloctionConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Resource
    private ExtTlThirdPartyPaymentMapper extTlThirdPartyPaymentMapper;
    @Autowired
    private IThirdPartyPaymentService thirdPartyPaymentService;

    @Autowired
    public GrayVersionConfig grayVersionConfig;


    @Bean
    public Job thirdPartyAlloctionJob() {
        return jobBuilderFactory.get("thirdPartyAlloctionJob")
                .start(thirdPartyAlloctionStep())
                .build();
    }

    @Bean
    public Step thirdPartyAlloctionStep() {
        return stepBuilderFactory.get("thirdPartyAlloctionStep")
                .tasklet(thirdPartyTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet thirdPartyTasklet() {
        return (stepContribution, chunkContext) -> {
            List<TlThirdPartyPayment> logList = extTlThirdPartyPaymentMapper.selectBySumStatus(TransBizConstant.PAYMENT_LOG_SUM_STATUS_T, grayVersionConfig.getCurrentVersion());
            List<CompletableFuture<Void>> list = logList.stream().map(log ->
                    CompletableFuture.runAsync(() ->
                            {
                                try {
                                    thirdPartyPaymentService.thirdPartyAllocation(log);
                                } catch (TxnBizException e) {
                                    logger.error("差错异常：",e);
                                }
                            }
                    )
            ).collect(Collectors.toList());

            long count = list.stream().map(CompletableFuture::join).count();
            logger.info("最终结果数{}",count);
            return RepeatStatus.FINISHED;
        };
    }
}
